package edu.pitt.dbmi.odie.gapp.gwt.client.analysis.management;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import edu.pitt.dbmi.odie.gapp.gwt.client.analysis.ODIE_AnalysisPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.analysis.datasource.ODIE_AnalysisDataSource;

public class ODIE_AnalysisManagementPanel extends VLayout {

	private ODIE_AnalysisPanel analysisPanel;
	
	private IButton navigateButton = null ;
	
	private IButton deleteButton = null ;
	
	private final DataSource dataSource = ODIE_AnalysisDataSource.getInstance();

	private final ListGrid listGrid = new ListGrid() {
		protected String getCellCSSText(ListGridRecord record, int rowNum,
				int colNum) {
			if (getFieldName(colNum).equalsIgnoreCase("progress")) {
				if (record.getAttributeAsString("analysisStatus")
						.equals("CODING")) {
					float requested = record.getAttributeAsInt("unitsRequested") ;
					float completed = record.getAttributeAsInt("unitsCompleted") ;
					float unitsLeftToRight = (100.0f * completed) / requested ;
					float unitsRightToLeft = 100.0f - unitsLeftToRight ;
					int rightBorderWidth = Math.round(unitsRightToLeft)  ;
					if (rightBorderWidth < 0) {
						rightBorderWidth = 0 ;
					}
					String cssStyle = "" ;
					cssStyle += "color: #DDFED1; " ;
					cssStyle += "background-color: #DDFED1; " ;
					cssStyle += "border-top-style: solid; " ;
					cssStyle += "border-top-width: 5px; " ;
					cssStyle += "border-top-color: #FFFFFF; " ;
					cssStyle += "border-bottom-style: solid; " ;
					cssStyle += "border-bottom-width: 5px; " ;
					cssStyle += "border-bottom-color: #FFFFFF; " ;
					cssStyle += "border-right-style: solid; " ;
					cssStyle += "border-right-width: " + rightBorderWidth + "px; " ;
					cssStyle += "border-right-color: #FFFFFF; " ;
					return cssStyle ;
				} else if (record.getAttributeAsString("analysisStatus")
						.equals("DELETING")) {
					float requested = record.getAttributeAsInt("unitsRequested") ;
					float completed = record.getAttributeAsInt("unitsCompleted") ;
					float unitsLeftToRight = (100.0f * completed) / requested ;
					float unitsRightToLeft = 100.0f - unitsLeftToRight ;
					int rightBorderWidth = Math.round(unitsRightToLeft)  ;
					if (rightBorderWidth < 0) {
						rightBorderWidth = 0 ;
					}
					String cssStyle = "" ;
					cssStyle += "color: #FFDFEF; " ;
					cssStyle += "background-color: #FFDFEF; " ;
					cssStyle += "border-top-style: solid; " ;
					cssStyle += "border-top-width: 5px; " ;
					cssStyle += "border-top-color: #FFFFFF; " ;
					cssStyle += "border-bottom-style: solid; " ;
					cssStyle += "border-bottom-width: 5px; " ;
					cssStyle += "border-bottom-color: #FFFFFF; " ;
					cssStyle += "border-right-style: solid; " ;
					cssStyle += "border-right-width: " + rightBorderWidth + "px; " ;
					cssStyle += "border-right-color: #FFFFFF; " ;
					return cssStyle ;
				} else {
					return super.getCellCSSText(record, rowNum, colNum);
				}
			} else {
				return super.getCellCSSText(record, rowNum, colNum);
			}
		}
	};

	private final DynamicForm form = new DynamicForm();
	
	public ODIE_AnalysisManagementPanel() {
	    
		listGrid.setWidth100();
		listGrid.setHeight(200);
		listGrid.setDataSource(dataSource);
		listGrid.setAutoFetchData(true);
		listGrid.setIsGroup(true) ;
		listGrid.setGroupTitle("Analyses") ;
		
		form.setIsGroup(true);
		form.setGroupTitle("Selected Analysis");
		form.setNumCols(6);
		form.setDataSource(dataSource);
		
		listGrid.addRecordClickHandler(new RecordClickHandler() {
			public void onRecordClick(RecordClickEvent event) {
				form.reset();
				form.editSelectedData(listGrid);
			}
		});
		
		HLayout actionPanel = buildActionPanel() ;
		actionPanel.setMembersMargin(20) ;
		addMember(listGrid);
		addMember(form);
		addMember(actionPanel);
	}
	
	private HLayout buildActionPanel() {
		
		HLayout actionPanel = new HLayout() ;
		actionPanel.setWidth100() ;
		
		this.navigateButton = new IButton("Navigate");
		this.navigateButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SC.say("Navigate to the selected record");
			}
		});
		actionPanel.addMember(this.navigateButton) ;
		
		this.deleteButton = new IButton("Delete");
		this.deleteButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SC.say("Delete selected record");
			}
		});
		actionPanel.addMember(this.deleteButton) ;
		
		return actionPanel ;
	}

	public void setAnalysisPanel(ODIE_AnalysisPanel analysisPanel) {
		this.analysisPanel = analysisPanel;
	}
	
	public ListGrid getListGrid() {
		return listGrid;
	}

}
